package com.lambkit.module.upms.client;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.JsonKit;
import com.lambkit.common.util.RequestUtils;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.api.UpmsApiService;
import com.lambkit.module.upms.record.UpmsLog;
import com.lambkit.web.LambkitResult;

public class UpmsLogService {
	
	private UpmsApiService getUpmsApiService() {
		return UpmsManager.me().getUpmsApiService();
	}

	public UpmsLog addRegistLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLog log = new UpmsLog();
		//log.setDescription("用户注册-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("注册成功");
		} else {
			log.setDescription("注册失败");
		}
		log.setUri("regist");
		log.setPermissions("regist");
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}

	public UpmsLog addLoginLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLog log = new UpmsLog();
		//log.setDescription("用户登录-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("登录成功");
		} else {
			log.setDescription("登录失败");
		}
		log.setUri("login");
		log.setPermissions("login");
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}

	private void toSave(UpmsLog log, String username, HttpServletRequest request) {
		if(request!=null) {
			log.setUrl(request.getRequestURI());
			log.setUserAgent(RequestUtils.getUserAgent(request));
			log.setMethod(request.getMethod());
			log.setIp(RequestUtils.getIpAddress(request));
			log.setBasePath(RequestUtils.getBasePath(request));
			log.setParameter(request.getQueryString());
			request.setAttribute("upmsLogForControllerAction", log);
		}
		log.setUsername(username);
		//log.setStartTime(DateTimeUtils.getCurrentTimeLong());
		//return log.save();
		//return UpmsLogModel.create(log).save();
		getUpmsApiService().insertUpmsLogSelective(log);
	}

	public UpmsLog addLogoutLog(String username, LambkitResult result, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLog log = new UpmsLog();
		//log.setDescription("用户退出-" + result.getCode() + "-" + result.getMessage());
		if(result.getCode()==1) {
			log.setDescription("退出成功");
		} else {
			log.setDescription("退出失败");
		}
		log.setUri("logout");
		log.setResult(JsonKit.toJson(result));
		log.setPermissions("logout");
		toSave(log, username, request);
		return log;
	}

	public UpmsLog addLog(String username, String desc, String uri, String result, String permissions, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLog log = new UpmsLog();
		log.setDescription(desc);
		log.setUri(uri);
		log.setPermissions(permissions);
		log.setResult(result);
		toSave(log, username, request);
		return log;
	}

	public UpmsLog addLog(String username, String desc, String uri, LambkitResult result, String permissions, HttpServletRequest request) {
		// TODO Auto-generated method stub
		UpmsLog log = new UpmsLog();
		log.setDescription(desc + "-" + result.getCode() + "-" + result.getMessage());
		log.setUri(uri);
		log.setPermissions(permissions);
		log.setResult(JsonKit.toJson(result));
		toSave(log, username, request);
		return log;
	}
}
